<?php
/**
 * Creates and applies filters for component output functions.
 *
 * @author Paul Gibbs <paul@byotos.com>
 * @version 2.0
 * @package Achievements 
 * @subpackage filters
 *
 * $Id: achievements-filters.php 821 2010-10-09 21:55:24Z DJPaul $
 */

/**
 * For reference, some WP filters you may want to use:
 *  - wp_filter_kses() VERY IMPORTANT
 *  - wptexturize()
 *  - convert_smilies()
 *  - convert_chars()
 *  - wpautop()
 *  - stripslashes_deep()
 *  - make_clickable()
 */

// Template tags
add_filter( 'dpa_get_achievement_name', 'wp_filter_kses', 1 );
add_filter( 'dpa_get_achievement_name', 'wptexturize' );
add_filter( 'dpa_get_achievement_name', 'convert_chars' );
add_filter( 'dpa_get_achievement_name', 'stripslashes_deep' );
add_filter( 'dpa_get_achievement_name', 'sanitize_text_field' );

add_filter( 'dpa_get_achievement_slug', 'wp_filter_kses', 1 );
add_filter( 'dpa_get_achievement_slug', 'force_balance_tags', 9 );
add_filter( 'dpa_get_achievement_slug', 'sanitize_title' );

if ( bp_is_active( 'activity' ) )
	add_filter( 'dpa_get_achievement_description', 'bp_activity_filter_kses', 1 );
else
	add_filter( 'dpa_get_achievement_description', 'wp_filter_kses', 1 );

add_filter( 'dpa_get_achievement_description', 'wptexturize' );
add_filter( 'dpa_get_achievement_description', 'convert_smilies' );
add_filter( 'dpa_get_achievement_description', 'convert_chars' );
add_filter( 'dpa_get_achievement_description', 'stripslashes_deep' );
add_filter( 'dpa_get_achievement_description', 'wpautop' );
add_filter( 'dpa_get_achievement_description', 'shortcode_unautop' );
add_filter( 'dpa_get_achievement_description', 'make_clickable' );
add_filter( 'dpa_get_achievement_description', 'do_shortcode' );

add_filter( 'dpa_get_achievement_id', 'intval' );
add_filter( 'dpa_get_achievement_action_id', 'intval' );
add_filter( 'dpa_get_achievement_picture_id', 'intval' );
add_filter( 'dpa_get_achievement_site_id', 'intval' );
add_filter( 'dpa_get_achievement_group_id', 'intval' );
add_filter( 'dpa_get_achievement_points', 'intval' );
add_filter( 'dpa_get_achievement_is_active', 'intval' );
add_filter( 'dpa_get_achievement_action_count', 'intval' );
add_filter( 'dpa_get_achievement_unlocked_count', 'intval' );
add_filter( 'dpa_get_achievement_action_count', 'intval' );
add_filter( 'dpa_get_achievement_progress_bar_width', 'intval' );

add_filter( 'dpa_get_total_achievement_count', 'bp_core_number_format' );
add_filter( 'dpa_get_total_achievement_count_for_user', 'bp_core_number_format' );
add_filter( 'dpa_get_member_achievements_score', 'bp_core_number_format' );


// Misc
add_filter( 'dpa_filter_generic_userid', 'intval' );

add_filter( 'dpa_get_addedit_action_description', 'wp_filter_kses', 1 );
add_filter( 'dpa_get_addedit_action_description', 'wptexturize' );
add_filter( 'dpa_get_addedit_action_description', 'convert_chars' );
add_filter( 'dpa_get_addedit_action_description', 'stripslashes_deep' );

add_filter( 'dpa_achievements_search_query', 'wp_filter_kses', 1 );
add_filter( 'dpa_achievements_search_query', 'wptexturize' );
add_filter( 'dpa_achievements_search_query', 'convert_chars' );
add_filter( 'dpa_achievements_search_query', 'stripslashes_deep' );

add_filter( 'dpa_admin_rss_feed', 'wp_filter_kses', 1 );
add_filter( 'dpa_admin_rss_feed', 'wptexturize' );
add_filter( 'dpa_admin_rss_feed', 'convert_chars' );
add_filter( 'dpa_admin_rss_feed', 'stripslashes_deep' );


// Editing
add_filter( 'dpa_achievement_name_before_save', 'wp_filter_kses', 1 );
add_filter( 'dpa_achievement_name_before_save', 'force_balance_tags' );
add_filter( 'dpa_achievement_name_before_save', 'sanitize_text_field' );

add_filter( 'dpa_achievement_slug_before_save', 'wp_filter_kses', 1 );
add_filter( 'dpa_achievement_slug_before_save', 'force_balance_tags', 9 );
add_filter( 'dpa_achievement_slug_before_save', 'sanitize_title' );

if ( bp_is_active( 'activity' ) )
	add_filter( 'dpa_achievement_description_before_save', 'bp_activity_filter_kses', 1 );
else
	add_filter( 'dpa_achievement_description_before_save', 'wp_filter_kses', 1 );

add_filter( 'dpa_achievement_description_before_save', 'force_balance_tags' );

add_filter( 'dpa_achievement_id_before_save', 'intval' );
add_filter( 'dpa_achievement_action_id_before_save', 'intval' );
add_filter( 'dpa_achievement_picture_id_before_save', 'intval' );
add_filter( 'dpa_achievement_action_count_before_save', 'intval' );
add_filter( 'dpa_achievement_points_before_save', 'intval' );
add_filter( 'dpa_achievement_is_active_before_save', 'intval' );
add_filter( 'dpa_achievement_site_id_before_save', 'intval' );
add_filter( 'dpa_achievement_group_id_before_save', 'intval' );

add_filter( 'dpa_addedit_warning', 'wp_filter_kses', 1 );
add_filter( 'dpa_addedit_warning', 'stripslashes' );


// Widgets
add_filter( 'dpa_widget_title', 'wp_filter_kses', 1 );
add_filter( 'dpa_widget_title', 'wptexturize' );
add_filter( 'dpa_widget_title', 'convert_chars' );
add_filter( 'dpa_widget_title', 'stripslashes_deep' );

add_filter( 'dpa_widget_achievement_slug', 'wp_filter_kses', 1 );
add_filter( 'dpa_widget_achievement_slug', 'wptexturize' );
add_filter( 'dpa_widget_achievement_slug', 'convert_chars' );
add_filter( 'dpa_widget_achievement_slug', 'stripslashes_deep' );

add_filter( 'dpa_widget_limit', 'intval' );

add_filter( 'dpa_widget_bool', 'absint' );


// WP Admin
add_filter( 'dpa_admin_settings_mediakeywords', 'wp_filter_kses', 1 );
add_filter( 'dpa_admin_settings_mediakeywords', 'wptexturize' );
add_filter( 'dpa_admin_settings_mediakeywords', 'convert_chars' );
add_filter( 'dpa_admin_settings_mediakeywords', 'stripslashes_deep' );

/**
 * Filters the bp_has_members() query to return only users who have unlocked the specific Achievement.
 *
 * @global object $bp BuddyPress global settings
 * @global wpdb $wpdb WordPress database object
 * @since 2.0
 */
function dpa_filter_users_by_achievement( $query, $sql_parts ) {
	global $bp, $wpdb;

	// Hacktastic. To override filter text on pagination. See bp_members_pagination_count().
	$find = __( 'Viewing member %1$s to %2$s (of %3$s active members)', 'buddypress' );  // Intentionally uses BuddyPress' text domain.
	$replace = __( 'Viewing member %1$s to %2$s (of %3$s members) who have unlocked this Achievement', 'dpa' );
	dpa_override_i18n( $find, $replace );

	if ( $bp->achievements->current_achievement->id ) {
		// This function is hooked into both bp_core_get_total_users_sql and bp_core_get_paged_users_sql, modify the query appropiately.
		if ( isset( $sql_parts['select_main'] ) )
	    $sql_parts['select_main'] .= ', unlocked.achieved_at';

   $sql_parts['where'] = ", {$bp->achievements->table_unlocked} as unlocked {$sql_parts['where']}" . $wpdb->prepare( ' AND unlocked.achievement_id = %d AND unlocked.user_id = u.ID', $bp->achievements->current_achievement->id );

		if ( "ORDER BY um.meta_value DESC" == $sql_parts[0] || "ORDER BY u.user_registered DESC" == $sql_parts[0] || "ORDER BY pd.value ASC" == $sql_parts[0] || "ORDER BY rand()" == $sql_parts[0] || "ORDER BY CONVERT(um.meta_value, SIGNED) DESC" == $sql_parts[0] )
			$sql_parts[0] = "ORDER BY unlocked.achieved_at DESC";
	}

	return apply_filters( 'dpa_filter_users_by_achievement', join( ' ', (array)$sql_parts ), $sql_parts );
}

/**
 * Changes the "last activity" timestamp in the members' template loop to when the Achievement was unlocked.
 * Use only in the members' template loop.
 *
 * @global BP_Core_Members_Template $members_template
 * @see dpa_filter_users_by_achievement()
 * @since 2.0
 */
function dpa_filter_unlockedby_activity_timestamp() {
	global $members_template;

	return sprintf( __( 'Unlocked %s ago', 'dpa' ), bp_core_time_since( $members_template->member->achieved_at ) );
}
?>